﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Cooksta_Data;

namespace Cooksta_Inventory_Manager
{
	public partial class MainForm : Form
	{
        private InventoryItem _currentItem;
        private bool _changesMade;
        private List<int> _itemIDs;
        private int _currentIndex;

		public MainForm()
		{
			InitializeComponent();
		}

        private void MainForm_Load(object sender, EventArgs e)
        {
            InventoryActions.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Projects\Cooksta_Inventory_Manager\Cooksta_Data\Inventory.mdf;Integrated Security=True;User Instance=True";
        }

        private void clearFields()
        {
            try
            {
                this.txtCost.Clear();
                this.txtDescription.Clear();
                this.txtInventoryCost.Clear();
                this.txtItemCode.Clear();
                this.txtPrice.Clear();
                this.txtQuantity.Clear();
                this.txtRevenueAmount.Clear();
                this.cboGroup.SelectedIndex = -1;
                this.chkActive.Checked = false;
                this._currentItem = null;
                this._changesMade = false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }

        private void saveItemInfo()
        {
            try
            {
                this._currentItem.Cost = Convert.ToDouble(this.txtCost.Text);
                this._currentItem.Description = this.txtDescription.Text;



                if (this._currentItem.ItemID == 0)
                    this._currentItem.ItemID = InventoryActions.Insert_Inventroy_Item(_currentItem);
                else
                    InventoryActions.Update_Inventroy_Item(_currentItem);

                _changesMade = false;
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }

        private void fillItemInfo(int itemID)
        {
            try
            {
                _currentItem = InventoryActions.Get_Inventory_Item_Info(itemID);
                txtRevenueAmount.Text = _currentItem.RevenueAmount.ToString("$#.##");
                txtCost.Text = _currentItem.Cost.ToString("$#.##");
                txtInventoryCost.Text = _currentItem.Quantity.ToString();
                txtPrice.Text = _currentItem.Price.ToString("$#.##");
                txtQuantity.Text = _currentItem.Quantity.ToString();
                txtItemCode.Text = _currentItem.ItemID.ToString();
                txtDescription.Text = _currentItem.Description;
                chkActive.Checked = _currentItem.IsActive;
                cboGroup.SelectedValue = _currentItem.GroupID;
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void fillTransactionGrid()
        {
            try
            {
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message, ex.InnerException);
            }
        }

       private bool saveConfirmation()
        {
            if(this._changesMade)
            {
                switch(MessageBox.Show("Would you like to save changes?","Save Changes?",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question))
                {
                    case DialogResult.Yes:
                        this.saveItemInfo();
                        return true;
                    case DialogResult.No:
                        return true;
                    case DialogResult.Cancel:
                        return false;
                    default:
                        return true;
                }
            }
            else
                return true;
        }

       private void processMovement(int direction)
       {
           this._currentIndex += direction;
           this._currentItem = InventoryActions.Get_Inventory_Item_Info(this._itemIDs[this._currentIndex]);

           if (this._currentIndex == 0)
               this.tsPrevious.Enabled = false;
           else if (this._currentIndex == this._itemIDs.Count - 1)
               this.tsNext.Enabled = false;
           else
           {
               this.tsNext.Enabled = true;
               this.tsPrevious.Enabled = true;
           }
       }


       private void handleChanges(object sender, EventArgs e)
       {
           this._changesMade = true;
       }

       private void toolStripButtonClick(object sender, EventArgs e)
        {
            try
            {
                switch (((ToolStripButton)sender).Name.ToUpper())
                {
                    case "TSSAVE":                        
                        break;
                    case "TSADD":
                        if (this.saveConfirmation())
                        {
                            this.clearFields();
                        }
                        break;
                    case "TSDELETECANCEL":
                        break;
                    case "TSPREVIOUS":
                        if (this.saveConfirmation())
                            this.processMovement(-1);
                        break;
                    case "TSNEXT":
                        if (this.saveConfirmation())
                            this.processMovement(1);
                        break;
                    case "TSSEARCH":
                        break;
                    case "TSEXIT":
                        break;
                    default:
                        break;
                }

              }
            catch(Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }

       private void btnPicture_Click(object sender, EventArgs e)
       {
           try
           {
               ofPicture.ShowDialog();
               if (ofPicture.FileNames.Length > 0)
               {
               
                   imgItem.ImageLocation = ofPicture.FileNames[0].ToString();
                   imgItem.SizeMode = PictureBoxSizeMode.StretchImage;
               }
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message, ex.InnerException);
           }
       }
	}
}
